<?php
/**
 * 登录
 *
 * 包括 登录 验证 退出 操作
 *
 * @copyright  Copyright (c) 2015-2025 NcPHP Inc. (http://www.ncphp.com)
 * @license    http://www.ncphp.com
 * @since      File available since Release v1.1
 */
defined('InNcPHP') or exit('Access Invalid!');

class LoginControl extends SystemControl{

	/**
	 * 不进行父类的登录验证，所以增加构造方法重写了父类的构造方法
	 */
	public function __construct(){
		Language::read('common,layout,login');
		$result = chksubmit(true,true,'num');
		if ($result){
		    if ($result === -11){
		        showMessage(L('invalid_request'));
		    }elseif ($result === -12){
		        showMessage(L('login_index_checkcode_wrong'));
		    }
		    if (processClass::islock('admin')) {
		        showMessage(L('login_index_operation_too'));
		    }
			//登录验证
			$obj_validate = new Validate();
			$obj_validate->validateparam = array(
			array("input"=>$_POST["user_name"],		"require"=>"true", "message"=>L('login_index_username_null')),
			array("input"=>$_POST["password"],		"require"=>"true", "message"=>L('login_index_password_null')),
			array("input"=>$_POST["captcha"],		"require"=>"true", "message"=>L('login_index_checkcode_null')),
			);
			$error = $obj_validate->validate();
			if ($error != ''){
				showMessage(L('error').$error);
			}else {
				$model_admin = Model('admin');
				$array	= array();
				$array['username']	= $_POST['user_name'];
				$array['password']	= md5(trim($_POST['password']));
//				die(json_encode($array));
				$admin_info = $model_admin->getAdminInfo($array);
				if(is_array($admin_info) and !empty($admin_info)) {
					//判断该商家是否正常启用
					if($admin_info['merchant_id']>0){
						/* @var member_merchantModel $merchant_model */
						$merchant_model = Model('member_merchant');
						$info = $merchant_model->getMerchantInfo(array('merchant_id'=>$admin_info['merchant_id'],'is_disable'=>0),'merchant_id');
						if($info['merchant_id']!=$admin_info['merchant_id']){
							processClass::addprocess('admin');
							showMessage('您的店铺暂时被禁用，请联系管理员处理','index.php?act=login');
							exit;
						}
					}
					$this->systemSetKey(array('name'=>$admin_info['username'], 'id'=>$admin_info['admin_id'],'gid'=>$admin_info['group_id'],'sp'=>$admin_info['is_super'],'merchant_id'=>$admin_info['merchant_id']));

					$update = array();
					$update['LoginNum']			= intval($admin_info['LoginNum']) + 1;
					$update['ThisLoginIP']		= getIp();
					$update['LastLoginIP']		= $admin_info['ThisLoginIP'];
					$update['ThisLoginTime']	= TIMESTAMP;
					$update['LastLoginTime']	= $admin_info['ThisLoginTime'];
					$model_admin->updateAdmin($array, $update);
					$this->log(L('nc_login'),1);
					processClass::clear('admin');
					@header('Location: index.php');
					exit();
				}else {
				    processClass::addprocess('admin');
					showMessage(L('login_index_username_password_wrong'),'index.php?act=login');
				}
			}
		}
		Tpl::output('html_title',$lang['login_index_need_login']);
		Tpl::showpage('login','login_layout');
	}
	public function loginOp(){}
	public function indexOp(){}
}